package com.alingrad.android.froyoengine.util;

import com.alingrad.android.froyoengine.tree.GameObject;

/**
 * Utility class that contains many helpful functions for the game engine.
 * @author alingrad
 *
 */
public abstract class MathHelper {
	/**
	 * Calculates the distance between two points.
	 * @param x1 x position of point 1.
	 * @param y1 y position of point 1.
	 * @param x2 x position of point 2.
	 * @param y2 y position of point 2.
	 * @return the distance between the two points.
	 */
	public static float distanceBetween(final float x1, final float y1, final float x2, final float y2) {
		return (float)Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
	}

	/**
	 * Calculates the distance between an object and a point.
	 * @param object1 the object.
	 * @param x x position of the point.
	 * @param y y position of the point.
	 * @return the distance between the object and the point.
	 */
	public static float distanceBetween(final GameObject object1, final float x, final float y) {
		return distanceBetween(object1.mX, object1.mY, x, y);
	}

	/**
	 * Calculates the distance between two objects.
	 * @param object1 object 1.
	 * @param object2 object 2.
	 * @return the distance between the two objects.
	 */
	public static float distanceBetween(final GameObject object1, final GameObject object2) {
		return distanceBetween(object1.mX, object1.mY, object2.mX, object2.mY);
	}

	/**
	 * Calculates the angle from one point to another.
	 * @param x1 x position of point 1.
	 * @param y1 y position of point 1.
	 * @param x2 x position of point 2.
	 * @param y2 y position of point 2.
	 * @return the angle from point 1 to point 2.
	 */
	public static float angleTo(final float x1, final float y1, final float x2, final float y2) {
		float angle = (float)Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
		while (angle >= 360) {
			angle -= 360;
		}
		while (angle < 0) {
			angle += 360;
		}
		return angle;
	}

	/**
	 * Calcultes the angle from an object to a point.
	 * @param object the object.
	 * @param x x position of the point.
	 * @param y y position of the point.
	 * @return the angle from the object to the point.
	 */
	public static float angleTo(final GameObject object, final float x, final float y) {
		return angleTo(object.mX, object.mY, x, y);
	}

	/**
	 * Calculates the angle from a point to an object.
	 * @param x x position of the point.
	 * @param y y position of the point.
	 * @param object the object.
	 * @return the angle from the point to the object.
	 */
	public static float angleTo(final float x, final float y, final GameObject object) {
		return angleTo(x, y, object.mX, object.mY);
	}

	/**
	 * Calculates the angle between two objects.
	 * @param object1 object 1.
	 * @param object2 object 2.
	 * @return the angle from object 1 to object 2.
	 */
	public static float angleTo(final GameObject object1, final GameObject object2) {
		return angleTo(object1.mX, object1.mY, object2.mX, object2.mY);
	}
}
